package com.internship.controller;

import java.io.IOException;
import java.util.List;
import java.util.Properties;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;





import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.internship.model.EnterpriseInfo;
import com.internship.model.EnterpriseUser;
import com.internship.model.JobStudent;
import com.internship.model.PracticeComment;
import com.internship.model.StudentInfo;
import com.internship.model.TeacherInfo;
import com.internship.model.Weeknote;
import com.internship.service.EnterpriseInfoService;
import com.internship.service.EnterpriseUserService;
import com.internship.service.JobStudentService;
import com.internship.service.PracticeCommentService;
import com.internship.service.StudentInfoService;
import com.internship.service.TeacherInfoService;
import com.internship.service.WeeknoteService;
import com.internship.util.ConfigProperties;



@Controller
@RequestMapping("practicecomment")
public class PracticeCommentController {
	private final Logger logger = Logger.getLogger(PracticeCommentController.class);
	
	@Autowired
	private StudentInfoService studentInfoService;
	@Autowired
	private EnterpriseInfoService 	enterpriseInfoService;
	@Autowired
	private TeacherInfoService teacherInfoService;
	@Autowired
	private EnterpriseUserService enterpriseUserService;
	@Autowired
	private JobStudentService 	jobStudentService;
	@Autowired
	private PracticeCommentService 	practiceCommentService;
	@Autowired
	private 	WeeknoteService weeknoteService;
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}
	/**?
	 * 实习报告页面
	 * @return
	 */
	@RequestMapping("/practice_report.do")
	public ModelAndView practiceReport(@RequestParam("studentNumber")String studentNumber,@RequestParam("pdf")int pdf) {
		ModelAndView mv = new ModelAndView("practice_report");
		JobStudent jobStudent = jobStudentService.getJobStudent(studentNumber);
		if (jobStudent!=null) {
			PracticeComment practiceComment = practiceCommentService.getPracticeCommentByStudentNumber(studentNumber);
			List<Weeknote> weeknoteList= weeknoteService.getWeeknoteReportList(studentNumber);
			StudentInfo studentInfo =studentInfoService.getStudentInfoByXH(studentNumber);
			EnterpriseInfo enterpriseInfo = enterpriseInfoService.getEnterpriseinfo(jobStudent.getPracticeEnterprise());
			EnterpriseUser enterpriseUser = enterpriseUserService.getEnterpriseuser(jobStudent.getEnterTeacherId());
			TeacherInfo teacherInfo =teacherInfoService.geTeacherInfoByZGH(jobStudent.getSchoolTeacher());
			mv.addObject("practiceComment",practiceComment);
			mv.addObject("jobStudent",jobStudent);
			mv.addObject("weeknoteList",weeknoteList);
			mv.addObject("studentInfo",studentInfo);
			mv.addObject("enterpriseUser",enterpriseUser);
			mv.addObject("enterpriseInfo",enterpriseInfo);
			mv.addObject("teacherInfo",teacherInfo);
			mv.addObject("pdf",pdf);
		}else {
			 mv.addObject("msg", " 您可能还未参加实习,暂无实习报告!");
			 mv.setViewName("error");
		}
		return mv;
	}
	/**?
	 * 导出pdf
	 */
	static Properties prop = ConfigProperties.getInstance().getConfigProperties();
	public  String getCommand(String htmlName , String pdfName){  
		String system = System.getProperty("os.name");
		if ("Linux".equalsIgnoreCase(system)) {
			 return   "wkhtmltopdf-amd64"+ " " + htmlName + " " + pdfName;
		}
        return  prop.getProperty("position") +" "+ htmlName + " " + pdfName;
    }  
	
	private String getBasePath() {
		HttpServletRequest request = getRequest();
		String path = request.getContextPath();
		String basePath =  request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		return basePath;
	}
	@RequestMapping("/pdf.do")
	public ModelAndView pdf(@RequestParam("studentNumber")String studentNumber){  
		ModelAndView mv = new ModelAndView("message");
		UUID uuid1 = UUID.randomUUID();
	        String command = getCommand(getBasePath()+"practicecomment/practice_report.do?studentNumber="+studentNumber+"&pdf="+1, getSession().getServletContext().getRealPath("/")+ "upload" + "/" +"/"+uuid1+".pdf");
	        try {  
	        	System.out.println(getBasePath()+"practicecomment/practice_report.do?studentNumber="+studentNumber+"&pdf="+1);
	            Runtime.getRuntime().exec(command); 
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }
	        mv.addObject("message","导出成功！");
			mv.addObject("uuid",uuid1);
			mv.addObject("download",1);
			return mv;
	    }    

	
}